<!--
  * 物料信息表
  *
  * @Author:    ADog
  * @Date:      2025-09-19 16:40:32
  * @Copyright  1.0
-->
<template>
  <a-modal
      :title="form.materialId ? '编辑' : '添加'"
      :width="600"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
        <a-form-item label="物料分类ID"  name="materialCategoryId">
          <a-input-number style="width: 100%" v-model:value="form.materialCategoryId" placeholder="物料分类ID" />
        </a-form-item>
        <a-form-item label="物料编码"  name="materialCode">
          <a-input style="width: 100%" v-model:value="form.materialCode" placeholder="物料编码" />
        </a-form-item>
        <a-form-item label="物料名称"  name="materialName">
          <a-input style="width: 100%" v-model:value="form.materialName" placeholder="物料名称" />
        </a-form-item>
        <a-form-item label="物料规格"  name="materialSpecification">
          <a-input style="width: 100%" v-model:value="form.materialSpecification" placeholder="物料规格" />
        </a-form-item>
        <a-form-item label="物料单位"  name="materialUnit">
          <a-input style="width: 100%" v-model:value="form.materialUnit" placeholder="物料单位" />
        </a-form-item>
        <a-form-item label="物料来源类型(0：自制 1：外购)"  name="materialSourceType">
          <a-input-number style="width: 100%" v-model:value="form.materialSourceType" placeholder="物料来源类型(0：自制 1：外购)" />
        </a-form-item>
        <a-form-item label="物料分类类型（0：原材料,1：半成品,2：产成品）"  name="materialCategoryType">
          <a-input-number style="width: 100%" v-model:value="form.materialCategoryType" placeholder="物料分类类型（0：原材料,1：半成品,2：产成品）" />
        </a-form-item>
        <a-form-item label="条形码"  name="materialBarcode">
          <a-input style="width: 100%" v-model:value="form.materialBarcode" placeholder="条形码" />
        </a-form-item>
        <a-form-item label="版本号（用于乐观锁）"  name="version">
          <a-input-number style="width: 100%" v-model:value="form.version" placeholder="版本号（用于乐观锁）" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/src/components/framework/smart-loading';
  import { materialApi } from '/src/api/business/core/material/material-api';
  import { smartSentry } from '/src/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      materialId: undefined, //主键
      materialCategoryId: undefined, //物料分类ID
      materialCode: undefined, //物料编码
      materialName: undefined, //物料名称
      materialSpecification: undefined, //物料规格
      materialUnit: undefined, //物料单位
      materialSourceType: undefined, //物料来源类型(0：自制 1：外购)
      materialCategoryType: undefined, //物料分类类型（0：原材料,1：半成品,2：产成品）
      materialBarcode: undefined, //条形码
      version: undefined, //版本号（用于乐观锁）
      deletedFlag: undefined, //删除状态
      disabledFlag: undefined, //禁用状态
      createTime: undefined, //创建时间
      updateTime: undefined, //更新时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
      materialCategoryId: [{ required: true, message: '物料分类ID 必填' }],
      materialName: [{ required: true, message: '物料名称 必填' }],
      materialSpecification: [{ required: true, message: '物料规格 必填' }],
      materialUnit: [{ required: true, message: '物料单位 必填' }],
      materialSourceType: [{ required: true, message: '物料来源类型(0：自制 1：外购) 必填' }],
      materialCategoryType: [{ required: true, message: '物料分类类型（0：原材料,1：半成品,2：产成品） 必填' }],
      materialBarcode: [{ required: true, message: '条形码 必填' }],
      version: [{ required: true, message: '版本号（用于乐观锁） 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.materialId) {
        await materialApi.update(form);
      } else {
        await materialApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
